package org.kungfu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenKit {
    //设置过期时间 7days
    private static final long EXPIRE_DATE = 7*24*60*60*1000;
    //token秘钥
    private static final String TOKEN_SECRET = "AZfasfhuaKungfuuGuwu2023NBNS";
 
    public static String token(String phoneNo, String code, String tokenSecret, long expireTime){
 
        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis() + expireTime);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(tokenSecret);
            //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");
            //携带username，password信息，生成签名
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("username", phoneNo)
                    .withClaim("password", code).withExpiresAt(date)
                    .sign(algorithm);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        return token;
    }

    public static String token(String phoneNo, String code, String tokenSecret) {
        return token(phoneNo, code, tokenSecret, EXPIRE_DATE);
    }

    public static String token(String phoneNo, String code, long expireTime) {
        return token(phoneNo, code, TOKEN_SECRET, expireTime);
    }

    public static String token(String phoneNo, String code) {
        return token(phoneNo, code, TOKEN_SECRET, EXPIRE_DATE);
    }

    public static boolean verify(String token) {
        return verify(token, TOKEN_SECRET);
    }

	public static boolean verify(String token, String tokenSecret) {

        try {
            Algorithm algorithm = Algorithm.HMAC256(tokenSecret);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

}